<template>
  <div class="app-container ZGYM">
    <div class="cardBox">
      <!-- <el-button
        @click="gobackWay"
        type="text"
        icon="el-icon-arrow-left"
        style="color: rgba(0, 0, 0, 0.85)"
        >返回</el-button
      > -->
      <!-- <el-divider direction="vertical"></el-divider> -->
      <span>用户默认配置项</span>
    </div>
    <div v-loading="loading" class="cardBox marginTop">
      <el-form
        :model="ruleForm"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="140px"
        class="demo-ruleForm"
      >
        <el-form-item label="用户头像" prop="avatar_arr">
          <logo-upload
            :fileList="avatarFile"
            :show-file-list="true"
            accept="image/jpeg, image/png"
            @uploadCallback="avatarCallback"
          />
        </el-form-item>
        <el-form-item label="是否强制会员登录" prop="is_force_mobile_login">
          <el-radio v-model="ruleForm.is_force_mobile_login" label="1"
            >开启</el-radio
          >
          <el-radio v-model="ruleForm.is_force_mobile_login" label="0"
            >关闭</el-radio
          >
          <div class="tips"></div>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="gradeSubmitForm('ruleForm')"
            >提交</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { getUserConfig, saveUserConfig } from "@/api/user/index";
import LogoUpload from "@/components/FileUpload";
export default {
  components: {
    LogoUpload,
  },
  data() {
    return {
      loading: false,
      avatarFile: [],
      ruleForm: {
        is_force_mobile_login: "1",
        avatar_arr: [],
      },
      rules: {
        is_force_mobile_login: [
          {
            required: true,
            validator: (rule, value, callback) => {
              if (value == "") {
                return callback(new Error("请选择是否启用"));
              }
              callback();
            },
            trigger: "change",
          },
        ],
        avatar_arr: [
          {
            required: true,
            validator: (rule, value, callback) => {
              callback();
            },
            trigger: "change",
          },
        ],
      },
    };
  },
  created() {
    this.getLevelConfigWay();
  },
  methods: {
    getLevelConfigWay() {
      this.loading = true;
      this.avatarFile = [];
      getUserConfig()
        .then((res) => {
          this.loading = false;
          if (res.code !== 200 || res.status !== 1) {
            this.$messages.error(res.msg);
            return;
          }
          res.data.avatar_arr.forEach((itme) => {
            this.avatarFile.push({
              url: itme,
            });
          });
          this.loading = false;
          this.ruleForm.is_force_mobile_login = String(
            res.data.is_force_mobile_login
          );
        })
        .catch(() => {
          this.loading = false;
        });
    },
    gradeSubmitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.loading = true;
          this.ruleForm.avatar_arr = [];
          this.avatarFile.forEach((item) => {
            this.ruleForm.avatar_arr.push(item.url);
          });
          saveUserConfig(this.ruleForm)
            .then((res) => {
              if (res.code !== 200) {
                this.$messages.error(res.msg);
                return;
              }
              this.$messages.success("已保存");
              this.getLevelConfigWay();
            })
            .catch(() => {
              this.getLevelConfigWay();
              this.loading = false;
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    // 上传头像调用
    avatarCallback(singleFile) {
      this.avatarFile = singleFile;
    },
  },
};
</script>

<style lang="scss" scoped>
.tips {
  margin-top: 0px;
  color: #c0c4cc;
  font-size: 12px;
  line-height: 26px;
}
</style>
<style>
.ZGYM .el-upload-list__item,
.ZGYM .el-upload--picture-card {
  width: 80px !important;
  height: 80px !important;
}

.ZGYM .el-upload--picture-card {
  line-height: 0 !important;
}

.ZGYM .el-icon-plus {
  line-height: 78px !important;
}
</style>